//
//  CommentsViewController.m
//  myhuibao
//
//  Created by lijun on 13-3-19.
//  Copyright (c) 2013年 myhuibao. All rights reserved.
//

#import "CommentsViewController.h"
#import <UIKit/UIKit.h>
#import "Config.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "SBJson.h"
#import "CommentsCell.h"

@interface CommentsViewController ()

@end

@implementation CommentsViewController
@synthesize maintable;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self requestData];
               
	// Do any additional setup after loading the view.
}
-(void) requestData{
    wqTable=maintable;
    pageIndex=1;
    NSString *spageIndex=[NSString stringWithFormat:@"%d",pageIndex];
    int merchantId=[[Config Instance] getMemberId];
    NSString *password=[[Config Instance] getPassword];
    NSString *sid=[NSString stringWithFormat:@"%d",merchantId];
    NSURL *url=[NSURL URLWithString:url_dealList];
    ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
    [request setPostValue:sid forKey:@"memberID"];
    [request setPostValue:password forKey:@"password"];
    [request setPostValue:spageIndex forKey:@"pageIndex"];
    [request setDelegate:self];
    [request startAsynchronous];
    [maintable reloadData];
}
- (void)viewDidUnload
{
    [super viewDidUnload];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

//异步请求的二个方法
-(void) requestFinished:(ASIFormDataRequest *)request
{
    NSString *jsonStr=[request responseString];
    NSDictionary *dic=[jsonStr JSONValue];
    tableDataArray = [dic valueForKey:@"data"];
    returnCount = [tableDataArray count];
    delegate=self;
    [maintable reloadData];
}
-(void) requestFailed:(ASIFormDataRequest *)request{
    NSError *error=[request error];
    NSLog(@"%@",error);
}
-(NSInteger )tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section{
    int count=[tableDataArray count];
    return count+1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}
- (CGFloat)tableView:(UITableView *)tableView widthForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 320;
}

-(UITableViewCell *) tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier=@"CommentsCellIdentifier";
    if([indexPath row]<([tableDataArray count])){
        CommentsCell *cell = (CommentsCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"CommentsCell" owner:nil options:nil];
        cell = [array objectAtIndex:0];
        [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
        NSDictionary *item = [tableDataArray objectAtIndex:indexPath.row];
        cell.merchantName = [item valueForKey:@"MERCHANTNAME"];
        cell.payType = [item valueForKey:@"PAYTYPENAME"] ;
        cell.dealValue = [item valueForKey:@"DEALVALUE"];
        cell.dealDate = [item valueForKey:@"DEALDATE"];
        return cell;
    }else{
        UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        //              if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                          reuseIdentifier:CellIdentifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        if(returnCount<pageSize){
            cell.textLabel.text=@"";
        }else{
            //创建loadMoreCell
            cell.textLabel.text=str_more;
        }
        cell.textLabel.textAlignment = UITextAlignmentCenter;
        cell.detailTextLabel.text=@"";
        cell.imageView.image =nil;
        return cell;
    }
}
- (IBAction)goBack:(id)sender {
    [super.navigationController
     popViewControllerAnimated:YES];
}
@end
